import Big from 'big.js';
export default function percent100(parts) {
  var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  var total = parts.reduce(function (acc, part) {
    return acc.plus(part);
  }, Big(0));
  var exacts = parts.map(function (part) {
    return Big(part).div(total).times(100);
  });
  var results = exacts.map(function (part) {
    return part.round(precision);
  });
  var sum = results.reduce(function (acc, part) {
    return acc.plus(part);
  }, Big(0));
  if (sum.eq(100)) {
    return results.map(function (item) {
      return item.toNumber();
    });
  }
  var diff = sum.minus(100);
  var diffs = results.map(function (result, i) {
    return {
      index: i,
      diff: Big(result).minus(exacts[i])
    };
  }).sort(function (a, b) {
    return b.diff.minus(a.diff).toNumber();
  });
  var number = diff.times(Big(10).pow(precision));
  var modifys = number.gt(0) ? diffs.slice(0, number.toNumber()) : diffs.slice(number.toNumber());
  var _loop = function _loop(i) {
    if (modifys.some(function (item) {
      return item.index === i;
    })) {
      if (number.gt(0)) {
        results[i] = results[i].minus(Big(0.1).pow(precision));
      } else {
        results[i] = results[i].plus(Big(0.1).pow(precision));
      }
    }
  };
  for (var i = 0; i < results.length; i++) {
    _loop(i);
  }
  return results.map(function (item) {
    return item.toNumber();
  });
}